/*
 * @ClassName language_api.dart
 * @Description TODO
 * @Author SuPeiSen
 * @Date 2020/7/17 12:25
 */

import 'package:flutter_su/common/app_color.dart';
import 'package:flutter_su/common/request.dart';
import 'package:flutter_su/common/show.dart';
import 'package:flutter_su/common/sp.dart';
import 'package:flutter_su/common/sp_key.dart';
import 'package:flutter_su/config/url_path.dart';
import 'package:flutter_su/enum/language_type.dart';
import 'package:flutter_su/model/setting_language_model.dart';

import 'base_api.dart';

class LanguageApi with BaseApi {
  // 设置语言
  Future<bool> settingLanguage(LanguageType languageType) async {
    Show.loading(
        bgColor: AppColor.MAIN_COLOR_CYAN, textColor: AppColor.MAIN_COLOR_BLUE);

    String type = languageType == LanguageType.chinese ? "zh_CN" : "en_US";
    String path = UrlPath.settingLanguage + type;
    DioResponseModel responseModel = await Request().get(path);
    if (responseModel.success == SuccessType.unSuccess) return false;
    var model = checkToModel<SettingLanguageModel>(
      obj: SettingLanguageModel(),
      map: responseModel.toJson,
    );
    if (model == nullModel) return false;
    // 保存本地token
    SpUtil.putString(SpKey.xToken, model.data.xToken);
    return true;
  }
}
